home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / CBGRX103.ZIP / contrib / libgrx / utils / fontdisp.c next >
Text File  |  1993-12-06  |  4KB  |  189 lines

  1. #include <grx.h>
  2. #include <mousex.h>
  3. #include <grxfont.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. int numchars;
  9. int numdisp;
  10. int startchr;
  11. int rows;
  12. int cols;
  13. int cellx;
  14. int celly;
  15. int xpos;
  16. int ypos;
  17.  
  18. GrTextOption option;
  19. GrFBoxColors colors;
  20.  
  21.  
  22. void displaychars(void)
  23. {
  24.     int chr = startchr;
  25.     int x = xpos;
  26.     int y = ypos;
  27.     int ii,jj;
  28.  
  29.     for(ii = 0; ii < rows; ii++) {
  30.         for(jj = 0; jj < cols; jj++) {
  31.         GrDrawChar(chr,x,y,&option);
  32.         x += cellx;
  33.         chr++;
  34.         }
  35.         x = xpos;
  36.         y += celly;
  37.     }
  38. }
  39.  
  40. void displayframe(void)
  41. {
  42.     GrFBoxColors cc = colors;
  43.     int x1,y1,x2,y2;
  44.     int pos;
  45.  
  46.     GrClearScreen(colors.fbx_intcolor);
  47.     x1 = xpos - 2;
  48.     y1 = ypos - 2;
  49.     x2 = x1 + cols*cellx;
  50.     y2 = y1 + rows*celly;
  51.     cc.fbx_intcolor = option.txo_bgcolor.v;
  52.     GrFramedBox(x1-2,y1-2,x2+2,y2+2,GrSizeX()/160,&cc);
  53.     for(pos = x1; pos <= x2; pos += cellx)
  54.         GrVLine(pos,y1,y2,option.txo_fgcolor.v);
  55.     for(pos = y1; pos <= y2; pos += celly)
  56.         GrHLine(x1,x2,pos,option.txo_fgcolor.v);
  57. }
  58.  
  59. int getpwr2(int number)
  60. {
  61.     int result = 1 << 14;
  62.  
  63.     while(result > number) result >>= 1;
  64.     return(result);
  65. }
  66.  
  67. int setup(GrFont *font)
  68. {
  69.     memset(&option,0,sizeof(option));
  70.     option.txo_font = font;
  71.     option.txo_xmag = 1;
  72.     option.txo_ymag = 1;
  73.     option.txo_fgcolor.v = GrBlack();
  74.     option.txo_bgcolor.v = GrWhite();
  75.     numchars = font->fnt_maxchar - font->fnt_minchar + 1;
  76.     if(getpwr2(numchars) != numchars)
  77.         numchars = getpwr2(numchars) << 1;
  78.     startchr = 0;
  79.     cellx = font->fnt_isfixed ?
  80.         font->fnt_width + 3 :
  81.         PFP(font)->pf_maxwidth + 3;
  82.     celly = font->fnt_height + 3;
  83.     rows = (GrSizeY() - 10) / celly;
  84.     cols = (GrSizeX() - 10) / cellx;
  85.     if((rows == 0) || (cols == 0))
  86.         return(0);
  87.     rows = getpwr2(rows);
  88.     cols = getpwr2(cols);
  89.     while((cols * rows) > numchars) {
  90.         cols >>= 1;
  91.         if((cols * rows) == numchars) break;
  92.         rows >>= 1;
  93.     }
  94.     xpos = (GrSizeX() - cols*cellx) / 2 + 2;
  95.     ypos = (GrSizeY() - rows*celly) / 2 + 2;
  96.     displayframe();
  97.     return(1);
  98. }
  99.  
  100. int displayfont(GrFont *font)
  101. {
  102.     if(!setup(font)) return(0);
  103.     displaychars();
  104.     getkey();
  105.     return(1);
  106. }
  107.  
  108. void setupcolors(void)
  109. {
  110.     int r = 0;
  111.     int g = 100;
  112.     int b = 160;
  113.  
  114.     colors.fbx_intcolor    = GrAllocColor(r,g,b);
  115.     colors.fbx_bottomcolor = GrAllocColor((7*r)/10,(7*g)/10,(7*b)/10);
  116.     colors.fbx_topcolor    = GrAllocColor((14*r)/10,(14*g)/10,(14*b)/10);
  117.     colors.fbx_leftcolor   = colors.fbx_topcolor;
  118.     colors.fbx_rightcolor  = colors.fbx_bottomcolor;
  119. }
  120.  
  121. void usage(void)
  122. {
  123.     fprintf(stderr,"usage: fontdisp [-m <width>x<height>] font ...\n");
  124.     exit(1);
  125. }
  126.  
  127. void main(int argc,char **argv)
  128. {
  129.     GrFont *f;
  130.     char *p;
  131.     char **fonts,**fp;
  132.     int  mode,w,h;
  133.     int  numfonts;
  134.  
  135.     fonts = malloc(sizeof(char *) * (argc + 3));
  136.     if(fonts == NULL) {
  137.         fprintf(stderr,"insufficient memory\n");
  138.         exit(1);
  139.     }
  140.     mode = GR_default_graphics;
  141.     w = h = 0;
  142.     numfonts = 0;
  143.     fp = fonts;
  144.     while(--argc > 0) {
  145.         if(*(p = *++argv) == '-') {
  146.         switch(p[1]) {
  147.           case 'm':
  148.           case 'M':
  149.             if(p[2] != '\0') p += 2;
  150.             else if(--argc == 0) usage();
  151.             else p = *++argv;
  152.             if(sscanf(p,"%dx%d",&w,&h) < 2) usage();
  153.             if((w < 320) || (h < 200)) usage();
  154.             mode = GR_width_height_graphics;
  155.             break;
  156.           default:
  157.             usage();
  158.         }
  159.         }
  160.         else {
  161.         *fp++ = p;
  162.         numfonts++;
  163.         }
  164.     }
  165.     if(numfonts == 0) {
  166.         *fp++ = "@:pc8x8.fnt";
  167.         *fp++ = "@:pc8x14.fnt";
  168.         *fp++ = "@:pc8x16.fnt";
  169.         numfonts = 3;
  170.     }
  171.     GrSetMode(mode,w,h);
  172.     setupcolors();
  173.     for(fp = fonts; --numfonts >= 0; fp++) {
  174.         if((f = GrLoadFont(*fp)) == NULL) {
  175.         GrSetMode(GR_default_text);
  176.         fprintf(stderr,"could not load font: %s\n",*fp);
  177.         exit(1);
  178.         }
  179.         if(!displayfont(f)) {
  180.         GrSetMode(GR_default_text);
  181.         fprintf(stderr,"could not display font: %s\n",*fp);
  182.         exit(1);
  183.         }
  184.     }
  185.     GrSetMode(GR_default_text);
  186.     exit(0);
  187. }
  188.  
  189.